import subprocess
import uiautomation as auto
import time

def run():
    # 打开微信
    subprocess.Popen("D:\WeChat\WeChat.exe")

    # 定位到主窗口
    wxMainWindow = auto.WindowControl(SearchDepth=1,ClassName="WeChatMainWndForPC",Name="微信")

    # 点击通讯录
    friendButton = wxMainWindow.ButtonControl(Name="通讯录")
    friendButton.Click()

    # 点击通讯录管理
    friendManagerButton = wxMainWindow.ButtonControl(Name="通讯录管理")
    friendManagerButton.Click()

    # 通讯录管理窗口
    friendManagerWindow = auto.WindowControl(Name="通讯录管理",ClassName="ContactManagerWindow")
    friendManagerWindow.MoveCursorToMyCenter()

    flag = True
    friends = []
    number = 1
    while flag:
        list = friendManagerWindow.ListControl(Name="")
        childrens = list.GetChildren()
        for item in childrens:
            nickName = item.TextControl().Name
            if nickName not in friends:
                print("""{}:{}""".format(number,nickName))
                number+=1
                friends.append(nickName)
        auto.WheelDown(waitTime=0.01)

if __name__=="__main__":
    run()